﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

public class PythonScriptCreator
{
    [MenuItem("Assets/Create/Python Script", false, 80)]
    public static void CreateNewPythonScript()
    {
        ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, 
            ScriptableObject.CreateInstance<DoCreatePythonScript>(),
            GetSelectedPathOrFallback() + "/NewPythonScript.py", 
            ThambNail(),
            // AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/unity-python/Gizmos/PythonScript Icon.png"),
            string.Empty);
    }

    public static string GetSelectedPathOrFallback()
    {
        string path = "Assets";
        foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
        {
            path = AssetDatabase.GetAssetPath(obj);
            if (!string.IsNullOrEmpty(path) && File.Exists(path))
            {
                path = Path.GetDirectoryName(path);
                break;
            }
        }
        return path;
    }

    public static Texture2D ThambNail()
    {
        string s = AssetDatabase.GUIDToAssetPath("4c671340c598941aab857b655a248a6a");
        // alignLeft = AssetDatabase.LoadAssetAtPath(tmproAssetFolderPath + "/Editor Resources/Textures/btn_AlignLeft.psd", typeof(Texture2D)) as Texture2D;
        if (string.IsNullOrEmpty(s))
            s = "Packages/com.unity.python/Gizmos/PythonScript Icon.png";
        return AssetDatabase.LoadAssetAtPath(s, typeof(Texture2D)) as Texture2D;
    }

    class DoCreatePythonScript : EndNameEditAction
    {
        static string TemplateText = @"
import PythonBehaviour
import UnityEngine
import unitypython
from unitypython import *

# Awake is called as object created
def Awake():
    pass
# Start is called before the first frame update
def Start():
    pass

# Update is called once per frame
def Update():
    pass

# OnEnable is called as script is actived 
def OnEnable():
    pass

# OnDisable is called as script is OnDisable 
def OnDisable():
    pass

# OnDestroy is called as script is OnDestroy 
def OnDestroy():
    pass

# OnCollisionEnter is called as script is OnCollisionEnter 
def OnCollisionEnter(c):
    pass

# OnCollisionStay is called as script is OnCollisionStay 
def OnCollisionStay(c):
    pass
# OnCollisionExit is called as script is OnCollisionExit 
def OnCollisionExit(c):
    pass
";

        public override void Action(int instanceId, string pathName, string resourceFile)
        {
            UnityEngine.Object o = CreateScriptAssetFromTemplate(pathName, resourceFile);
            ProjectWindowUtil.ShowCreatedAsset(o);
        }

        internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile)
        {
            string fullPath = Path.GetFullPath(pathName);
            string text = TemplateText;
            // string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName);
            // text = Regex.Replace(text, "#NAME#", fileNameWithoutExtension);
            bool encoderShouldEmitUTF8Identifier = true;
            bool throwOnInvalidBytes = false;
            UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes);
            bool append = false;
            StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding);
            streamWriter.Write(text);
            streamWriter.Close();
            AssetDatabase.ImportAsset(pathName);
            return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
        }
    }
}